package com.liu.resume.ui;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.liu.resume.MyApplication;
import com.liu.resume.MyEvent;
import com.liu.resume.R;
import com.liu.resume.ui.activity.ApkFragment;
import com.liu.resume.ui.activity.ContactsFragment;
import com.liu.resume.ui.activity.LoginActivity;
import com.liu.resume.ui.activity.ZoomOutPageTransformer;
import com.liu.resume.ui.base.BaseActivity;

import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {

    @Bind(R.id.statusBar)
    View statusBar;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_login)
    TextView tvLogin;
    @Bind(R.id.container)
    ViewPager mPager;

    private SectionsPagerAdapter mAdapter;
    private int mCreateTranslationY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerEventBus();
        toolbar.setNavigationIcon(R.mipmap.ic_logo_white_64);
        toolbar.setOnMenuItemClickListener(this);
        toolbar.inflateMenu(R.menu.menu_main);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        Log.e("main", "initView");
        if (MyApplication.INSTANCE.isLogin) {
            tvLogin.setVisibility(View.GONE);
            mPager.setVisibility(View.VISIBLE);
            mAdapter = new SectionsPagerAdapter(getFragmentManager());
            mPager.setAdapter(mAdapter);
            mPager.setOnPageChangeListener(mAdapter);
            mPager.setPageTransformer(true, new ZoomOutPageTransformer());

            // Get the Create button Translation 56+16 dp
            mCreateTranslationY = (int) (getResources().getDisplayMetrics().density * 72);
        } else {
            tvLogin.setVisibility(View.VISIBLE);
            mPager.setVisibility(View.GONE);
        }
    }

    /*
      * 更新用户
      * */
    @Subscribe
    public void onEventMainThread(MyEvent event) {
        if (event == MyEvent.UPDATE_USER) {
            initView();
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }


    @OnClick(R.id.tv_login)
    public void onClick() {
        LoginActivity.actionStart(MainActivity.this);
    }


    class SectionsPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
        private HomeFragment homeFragment;
        private ApkFragment apkFragment;
        private ContactsFragment contactsFragment;

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            if (position == 0)
                return homeFragment == null ? homeFragment = new HomeFragment() : homeFragment;
            else if (position == 1)
                return apkFragment == null ? apkFragment = new ApkFragment() : apkFragment;
            else if (position == 2)
                return contactsFragment == null ? contactsFragment = new ContactsFragment() : contactsFragment;
            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            if (position == 1)
                apkFragment = null;
            else if (position == 0) {
                homeFragment = null;
            } else if (position == 2)
                contactsFragment = null;
            super.destroyItem(container, position, object);
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            int tY = 0;
            if (position == 0) {
                tY = (int) (mCreateTranslationY - mCreateTranslationY * positionOffset);
            }
        }

        @Override
        public void onPageSelected(int position) {
            /*
            if (position == 0) {
                mCreate.animate()
                        .translationYBy(mCreate.getTranslationY())
                        .translationY(mCreateTranslationY)
                        .setDuration(320)
                        .start();
            } else {
                mCreate.animate()
                        .translationYBy(mCreate.getTranslationY())
                        .translationY(0)
                        .setDuration(320)
                        .start();
            }
            */
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }
}
